﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pets.Abstract;
using System.IO;
namespace Pets.Concrete
{
    /// <summary>
    ///  Save\load to  List<T> from .cvs file
    /// </summary>
   public sealed class SerializerCVS : ISerializer
   {
       #region Interface's methods

       public List<T> Load<T>(string filename) where T : class, new()
        {
            StreamReader st_read = new StreamReader(filename);
            List<T> data = new List<T>();
            int index_data = -1;
            string[] split_props;
            string line;
            while ((line = st_read.ReadLine()) != null)
            {
                split_props = line.Split(new char[] { ';' });
                index_data++;
                Type t = Type.GetType(split_props[0].Trim());
                data.Add(Activator.CreateInstance(t) as T);
                for (int i = 1; i < split_props.Length - 1; i++)
                {
                    if (split_props[i].Trim() != "null")
                    {
                        Type prop_type = data[index_data].GetType().GetProperties()[i - 1].PropertyType;
                        if (prop_type == typeof(System.String))
                        {
                            data[index_data].GetType().GetProperties()[i - 1].SetValue(data[index_data], split_props[i].Trim(), null);
                        }
                        if(prop_type == typeof(System.Int32))
                        {
                            data[index_data].GetType().GetProperties()[i - 1].SetValue(data[index_data], Convert.ToInt32(split_props[i].Trim()), null);
                        }
                    }
                }
            }
            st_read.Close();

            return data;
        }

       public void Save<T>(string filename, List<T> data) where T : class, new()
        {
            StreamWriter st_write = new StreamWriter(filename, false);
            
           /// Review elements in data
            foreach (T item in data)
            {
                string out_string = "";
                out_string += item.GetType().ToString() + "; ";

                // Review item's proprties 
                foreach (System.Reflection.PropertyInfo item_property in item.GetType().GetProperties())
                {
                    // If value - composite - ignore
                    if (item_property.PropertyType == typeof(int) || item_property.PropertyType == typeof(string))
                    {
                        out_string += item_property.GetValue(item, null) + "; ";
                    }

                    // Else set value
                    else
                    {
                        out_string += "null; ";
                    }
                }

                // Write to file
                st_write.WriteLine(out_string);
            }
            st_write.Close();
        }

       #endregion
   }
}
